package com.rc.saas.tenant.service.acct;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.acct.AcctRuleMapper;
import com.rc.saas.tenant.mapper.acct.AcctSubItemMapper;
import com.rc.saas.tenant.mapper.acct.TenantOperatorAcctSettingMapper;
import com.rc.saas.tenant.model.acct.*;
import com.rc.saas.tenant.model.operator.TenantOperatorStation;
import com.rc.saas.tenant.search.acct.AcctRuleSearch;
import com.rc.saas.tenant.service.operator.TenantOperatorStationService;
import com.rc.saas.tenant.vo.acct.AcctRuleResultVo;
import com.rc.saas.tenant.vo.acct.AcctRuleSaveVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author frog
 * @date 2021/1/19
 */
@Service
public class AcctRuleServiceImpl implements AcctRuleService {

    @Autowired
    private AcctRuleMapper acctRuleMapper;

    @Autowired
    private TenantOperatorAcctSettingMapper tenantOperatorAcctSettingMapper;

    @Autowired
    private TenantOperatorStationService tenantOperatorStationService;

    @Autowired
    private AcctSubItemMapper acctSubItemMapper;

    @Override
    public AcctRuleMapper getMapper() {
        return acctRuleMapper;
    }

    @Override
    public TResponse saveAcctRule(AcctRuleSaveVo acctRuleSaveVo) throws ParseException {
        AcctRule acctRule = new AcctRule();
        TenantOperatorAcctSetting tenantOperatorAcctSetting = new TenantOperatorAcctSetting();
        BeanUtils.copyProperties(acctRuleSaveVo, acctRule);
        if (acctRuleSaveVo.getAcctRuleId() == null) {
            acctRuleMapper.insertReturnId(acctRule);

            //新增结算子项
            String subItems=acctRuleSaveVo.getAcctSubItems();
            List<String> items=Arrays.asList(subItems.split(","));
//            List<String> listObjectFir = (List<String>) JSONArray.parse(acctRuleSaveVo.getAcctSubItems());
            for (int i=0;i<items.size();i++){
                AcctSubItem acctSubItem=new AcctSubItem();
                acctSubItem.setAcctRuleId(acctRule.getAcctRuleId());
                acctSubItem.setTenantCode(acctRule.getTenantCode());
                acctSubItem.setTenantOperatorCode(acctRule.getTenantOperatorCode());
                acctSubItem.setOperatorStationId(acctRule.getOperatorStationId());
                String item=items.get(i);
                acctSubItem.setSubItem(item);
                acctSubItemMapper.insertReturnId(acctSubItem);
            }

            TenantOperatorStation tenantOperatorStation = tenantOperatorStationService.selectByPrimaryKey(acctRuleSaveVo.getOperatorStationId());
            tenantOperatorAcctSetting.setElecStationId(tenantOperatorStation.getElecStationId());
            tenantOperatorAcctSetting.setAcctRuleId(acctRule.getAcctRuleId());
            tenantOperatorAcctSetting.setTenantOperatorCode(acctRule.getTenantOperatorCode());
            tenantOperatorAcctSetting.setTenantCode(acctRule.getTenantCode());
            tenantOperatorAcctSetting.setSettlementMethod(acctRuleSaveVo.getSettlementMethod());

            //定义结算时间
            //String acctDay = "2020-01-" + acctRuleSaveVo.getAcctDay();
            if (updateSettingTime(acctRuleSaveVo, tenantOperatorAcctSetting)) return TResponse.FAIL("请输入正确的时间");

            tenantOperatorAcctSettingMapper.insertSelective(tenantOperatorAcctSetting);
        } else {
            acctRuleMapper.updateByPrimaryKeySelective(acctRuleSaveVo);
            //获取结算子项
            String subItems=acctRuleSaveVo.getAcctSubItems();
            List<String> items=Arrays.asList(subItems.split(","));
            //删除子项
            AcctSubItemExample acctSubItemExample=new AcctSubItemExample();
            acctSubItemExample.createCriteria().andAcctRuleIdEqualTo(acctRuleSaveVo.getAcctRuleId()).andTenantCodeEqualTo(acctRuleSaveVo.getTenantCode()).andOperatorStationIdEqualTo(acctRuleSaveVo.getOperatorStationId()).andTenantOperatorCodeEqualTo(acctRuleSaveVo.getTenantOperatorCode());
            int result=acctSubItemMapper.deleteByExample(acctSubItemExample);
            System.out.println(result);

            for (int i=0;i<items.size();i++){
                AcctSubItem subItem=new AcctSubItem();
                subItem.setAcctRuleId(acctRule.getAcctRuleId());
                subItem.setTenantCode(acctRule.getTenantCode());
                subItem.setTenantOperatorCode(acctRule.getTenantOperatorCode());
                subItem.setOperatorStationId(acctRule.getOperatorStationId());
                String item=items.get(i);
                subItem.setSubItem(item);
                //新增子项
                acctSubItemMapper.insertReturnId(subItem);
            }


            TenantOperatorAcctSettingExample tenantOperatorAcctSettingExample = new TenantOperatorAcctSettingExample();
            tenantOperatorAcctSettingExample.createCriteria().andAcctRuleIdEqualTo(acctRuleSaveVo.getAcctRuleId());
            List<TenantOperatorAcctSetting> tenantOperatorAcctSettings = tenantOperatorAcctSettingMapper.selectByExample(tenantOperatorAcctSettingExample);
            if (CollectionUtils.isEmpty(tenantOperatorAcctSettings)) {
                return TResponse.FAIL("tenantOperatorAcctSettings为空");
            }
            if (updateSettingTime(acctRuleSaveVo, tenantOperatorAcctSettings.get(0))) return TResponse.FAIL("请输入正确的时间");

            TenantOperatorAcctSettingExample example = new TenantOperatorAcctSettingExample();
            example.createCriteria().andAcctRuleIdEqualTo(tenantOperatorAcctSettings.get(0).getAcctRuleId());
            tenantOperatorAcctSettingMapper.updateByExampleSelective(tenantOperatorAcctSettings.get(0), example);
        }

        return TResponse.SUCCESS();
    }

    private boolean updateSettingTime(AcctRuleSaveVo acctRuleSaveVo, TenantOperatorAcctSetting tenantOperatorAcctSetting) throws ParseException {
        //定义结算时间
        //String acctDay = "2020-01-" + acctRuleSaveVo.getAcctDay();
        SimpleDateFormat format = new SimpleDateFormat("dd");
        if (StringUtil.isBlank(acctRuleSaveVo.getRunTime())) {
            return true;
        }
        if (StringUtil.isNotBlank(acctRuleSaveVo.getAcctDay())){
            tenantOperatorAcctSetting.setAcctDay(format.parse(acctRuleSaveVo.getAcctDay()));
        }
        if (StringUtil.isNotBlank(acctRuleSaveVo.getHalfMonthDay())) {
            tenantOperatorAcctSetting.setHalfMonthDay(format.parse(acctRuleSaveVo.getHalfMonthDay()));
        }
        format = new SimpleDateFormat("HH");
        tenantOperatorAcctSetting.setRunTime(format.parse(acctRuleSaveVo.getRunTime()));
        return false;
    }

    @Override
    public void updateAcctRule(AcctRuleSaveVo acctRuleSaveVo) {

    }

    @Override
    public PageInfo<AcctRuleResultVo> listPageAcctRule(AcctRuleSearch acctRuleSearch) {
        PageHelper.startPage(acctRuleSearch.getOffset(), acctRuleSearch.getLimit());
        List<AcctRuleResultVo> list = acctRuleMapper.listPageAcctRule(acctRuleSearch);
        return new PageInfo<AcctRuleResultVo>(list);
    }

    @Override
    public Boolean isExecuteAcctRule(AcctRuleSearch acctRuleSearch) {
        if (StringUtil.isBlank(acctRuleSearch.getStartTime()) && StringUtil.isBlank(acctRuleSearch.getEndTime())) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = format.format(System.currentTimeMillis());
            acctRuleSearch.setEndTime(date);
            acctRuleSearch.setStartTime(date);
        }
        acctRuleSearch.setOperatorStationId(acctRuleSearch.getOperatorStationId());
        acctRuleSearch.setStatus("0");
        List<AcctRuleResultVo> list = acctRuleMapper.isExecuteAcctRule(acctRuleSearch);
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        return true;
    }

    @Override
    public AcctRuleResultVo selectAcctRule(Long acctRuleId) {
        return acctRuleMapper.selectAcctRule(acctRuleId);
    }

    @Override
    public Boolean isProcessingAcctRule(Long acctRuleId, Long operatorStationId, Date ruleStartTime, Date ruleEndTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<AcctRuleResultVo> list = acctRuleMapper.isProcessingAcctRule(acctRuleId, operatorStationId, format.format(ruleStartTime), format.format(ruleEndTime));
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        return true;
    }

    @Override
    public int countByAcctRuleStation(String tenantCode) {
        return acctRuleMapper.countByAcctRuleStation(tenantCode);
    }

}
